package com.bocek.chakmapoint.view.slide;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Iterator;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.bocek.chakmapoint.controller.SlideEditController;
import com.bocek.chakmapoint.controller.SlideshowController;
import com.bocek.chakmapoint.model.SlideItem;
import com.bocek.chakmapoint.model.SlideStyleList;

/**
 * Panel for drawing current slide contents.
 * 
 * @author  Baris Sencan
 * @version 1.2
 */
public class SlideView extends JPanel {
	private static final long serialVersionUID = -4565217850230507980L;
	private static final SlideView instance = new SlideView();
	
	public static SlideView getInstance() {
		return instance;
	}

	/**
	 * Create the panel.
	 * 
	 * @requires Nothing.
	 * @modifies this
	 * @effects  Nothing.
	 */
	private SlideView() {
		setFocusable(true);
		
		addMouseListener(SlideEditController.getInstance());
		addMouseMotionListener(SlideEditController.getInstance());
		addKeyListener(SlideEditController.getInstance());
		
		setBackground(Color.WHITE);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		
		SlideEditController.getInstance().setDrawPanel(this);
	}
	
	/**
	 * Draw slide items.
	 * 
	 * @requires g != null
	 * @modifies g
	 * @effects  Nothing.
	 */
	protected void paintComponent(Graphics g) {
		Iterator<SlideItem> iterator = null;
		SlideItem current = null;
		int styleIndex = SlideStyleList.getInstance().getCurrent();
		
		super.paintComponent(g);
		
		g.setColor(SlideStyleList.getInstance().getStyle(styleIndex).getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(SlideStyleList.getInstance().getStyle(styleIndex).getColor());
		
		iterator = SlideEditController.getInstance().getItemIterator();
		
		// Draw slide items.
		while (iterator != null && iterator.hasNext()) {
			iterator.next().draw(g);
		}
		
		// Draw current edit.
		current = SlideEditController.getInstance().getCurrentCreation();
		if (current != null) current.draw(g);
		
		// Draw slide number.
		if (SlideEditController.getInstance().isNumberShown())
			g.drawString(
					Integer.toString(SlideEditController.getInstance().getCurrentSlideNo()),
					10,
					470);
	}
}
